/**
 * @title AbsMatchListAdapter.java
 * @package com.esun.cheetah.view.match.matchlist.adapter
 * @description 抽象的比赛列表数据适配器
 * @author liugan
 * @date 2012-4-10
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.match.matchlist.adapter;

import java.util.ArrayList;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.attention.AddAttentionMatch;
import com.esun.cheetah.biz.personal.attention.AttentionListManager;
import com.esun.cheetah.biz.personal.attention.CancelAttenMatch;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.model.match.matchlist.Match;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * @title AbsMatchListAdapter
 * @description 抽象的比赛列表数据适配器
 * @author liugan
 */
public abstract class AbsMatchListAdapter extends BaseAdapter {
	/**
	 * @field attentionIsDoing
	 * @description 标识是否正在请求关注。正在请求时，将不响应用户的点击事件。
	 */
	public static boolean attentionIsDoing;

	/**
	 * @field markView
	 * @description 用户点击了的ImageView
	 */
	public static ImageView markView;

	/**
	 * @field matchInfo
	 * @description 相关的比赛信息，指向缓存的引用
	 */
	public static Match matchInfo;

	protected Context context;

	/**
	 * @field doingCount
	 * @description 正在进行的比赛数目
	 */
	protected int doingCount;

	/**
	 * @field doneCount
	 * @description 已完场的比赛数目
	 */
	protected int doneCount;
	/**
	 * @field futureCount
	 * @description 未开始的比赛数目
	 */
	protected int futureCount;

	/**
	 * @field markHandler
	 * @description 处理请求关注的handler
	 */
	protected Handler markHandler;

	/**
	 * @field markHandler
	 * @description 处理取消关注的handler
	 */
	protected Handler unMarkHandler;

	/**
	 * @field countListener
	 * @description 监听统计比赛进行情况
	 */
	protected OnCountListener countListener;

	public AbsMatchListAdapter(Context context) {
		this.context = context;
		markHandler = new MarkHandler(context.getMainLooper());
		unMarkHandler = new CancelMarkHandler(context.getMainLooper());
	}

	public void setOnCountListener(OnCountListener onCountListener) {
		this.countListener = onCountListener;
	}

	public abstract void setList(
			ArrayList<Match> list);

	public abstract ArrayList<Match> getList();

	@Override
	public int getCount() {
		if (getList() == null) {
			return 0;
		}
		return getList().size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	/**
	 * @title updateStatistics
	 * @description 根据比赛状态统计，必须在UI线程中执行
	 */
	public void updateStatistics() {
		doneCount = 0;
		futureCount = 0;
		doingCount = 0;
		for (Match info : getList()) {
			switch (info.getShowState()) {
			case over:
				doneCount++;
				break;
			case unplayed:
				futureCount++;
				break;
			case playing:
				doingCount++;
				break;
			default:
				break;
			}
		}
		if (countListener != null) {
			doCallback();
		}
	}

	public void doCallback() {
		if (getTotalCount() == 0) {
			countListener.onEmpty(true);
		} else {
			countListener.onEmpty(false);
			countListener.postCount(getTotalCount(), doingCount, doneCount,
					futureCount);
		}

	}

	public int getDoingCount() {
		return doingCount;
	}

	public int getDoneCount() {
		return doneCount;
	}

	public int getFutureCount() {
		return futureCount;
	}

	public int getTotalCount() {
		return doingCount + doneCount + futureCount;
	}

	/**
	 * @title clear
	 * @description 情况适配器中的数据
	 */
	public abstract void clear();

	/**
	 * @title OnCountListener
	 * @description 比赛状态统计信息监听器
	 * @author liugan
	 */
	public interface OnCountListener {
		/**
		 * @title onEmpty
		 * @description 是否没有比赛的情况
		 * @param noMatch
		 */
		void onEmpty(boolean noMatch);

		/**
		 * @title postCount
		 * @description 比赛统计正常完成后
		 * @param total
		 * @param doing
		 * @param done
		 * @param future
		 */
		void postCount(int total, int doing, int done, int future);
	}

	/**
	 * @title MarkHandler
	 * @description 请求关注比赛的Handler类
	 * @author liugan
	 */
	class MarkHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			attentionIsDoing = false;
			if (msg.what == Config.COMMON_INFO_MSG_20001) {
				markView.setImageResource(R.drawable.s_ib_star_marked);
				matchInfo.setMarked(true);
//				MatchListManager.getInstance().updateAttentionState(
//						matchInfo.getMatchId(), true);
				Toast.makeText(context, "关注成功", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(context, "关注失败", Toast.LENGTH_SHORT).show();
			}
			super.handleMessage(msg);
		}

		public MarkHandler(Looper looper) {
			super(looper);
		}

	}

	/**
	 * @title CancelMarkHandler
	 * @description 请求取消关注的handler类
	 * @author liugan
	 */
	class CancelMarkHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			attentionIsDoing = false;
			if (msg.what == Config.COMMUNICATER_SUCCESS) {
				markView.setImageResource(R.drawable.s_ib_star_unmarked);
				matchInfo.setMarked(false);
				// 更新本地关注列表
				AttentionListManager.getInstance().updateCancelAttention(
						matchInfo.getMatchId());
//				MatchListManager.getInstance().updateAttentionState(
//						matchInfo.getMatchId(), false);
				Toast.makeText(context, "取消成功", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(context, "取消失败", Toast.LENGTH_SHORT).show();
			}
			super.handleMessage(msg);
		}

		public CancelMarkHandler(Looper looper) {
			super(looper);
		}
	}

	public class OnMarkButtonClickListener implements View.OnClickListener {
		Match info;

		public OnMarkButtonClickListener(Match info) {
			this.info = info;
		}

		@Override
		public void onClick(View v) {
			if (!TextUtils.isEmpty(UserInfo.getUserInfo().getCk())) {
				if (!attentionIsDoing) {
					markView = (ImageView) v;
					attentionIsDoing = true;
					matchInfo = info;
					String userName = UserInfo.getUserInfo().getUserName();
					String matchId = matchInfo.getMatchId();
					String hostTeam = matchInfo.getHomeName();
					String guestTeam = matchInfo.getAwayName();
					String beginTime = matchInfo.getMatchDate() + " "
							+ matchInfo.getMatchTime() + ":00";
					if (matchInfo.isMarked()) {
						new CancelAttenMatch(unMarkHandler, matchId)
								.startCommunicate();
					} else {
						new AddAttentionMatch(markHandler, userName,
								Key.KEY_FOOTBALL, matchId, hostTeam, guestTeam,
								beginTime).startCommunicate();
					}
				}

			} else {
				Toast.makeText(context, "请登录!", Toast.LENGTH_SHORT).show();
			}

		}
	}

}
